﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using System.Data.SqlClient;
using System.Data.Common;

namespace CH.Study.ConsoleApp.ExpressionTree
{
    public class QueryExpressionSolution
    {
        public Expression Expression;
        public string CmdText { get; set; }
        public DbParameter[] Parameter { get; set; }
        public string CountCmdText { get; set; }
        public DbParameter[] CountParameter { get; set; }
        public QueryExpressionType ExpressionType { get; set; }
        public Type ReturnType { get; set; }
        public bool IsPage { get; set; }
        private QueryExpressionAnalyse queryExpressionAnalyse;
        public QueryExpressionSolution(QueryExpressionTranslator queryExpressionTranslator, Expression expression)
        {
            this.queryExpressionAnalyse = new QueryExpressionAnalyse();
            this.Expression = this.queryExpressionAnalyse.Analyse(expression);
            this.CmdText = queryExpressionTranslator.Translate(this.Expression);
            this.Parameter = queryExpressionTranslator.GetParameter();
            this.ExpressionType = (QueryExpressionType)this.Expression.NodeType;
            this.ReturnType = this.Expression.Type;

            switch (this.ExpressionType)
            {
                case QueryExpressionType.Select:
                    SelectExpression se = this.Expression as SelectExpression;
                    this.IsPage = se.IsPage;
                    if (this.IsPage)
                    {
                        se.ColumnExp = new ColumnExpression(Expression.Call(null, typeof(ExpressionMethodExtend).GetMethod("Count"), Expression.Parameter(typeof(int?))));
                        se.SkipExp = new SkipExpression(null);
                        se.TakeExp = new TakeExpression(null);
                        se.GroupExp = new GroupExpression(null);
                        se.OrderExp = new OrderExpression();
                        this.CountCmdText = queryExpressionTranslator.Translate(this.Expression);
                        this.CountParameter = queryExpressionTranslator.GetParameter();
                    }
                    break;
                case QueryExpressionType.Insert:
                    break;
                case QueryExpressionType.Update:
                    break;
                case QueryExpressionType.Delete:
                    break;
                default:
                    break;
            }

            

            
        }
    }
}
